package com.supermap.business.cloud.api;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.redis.JedisComponent;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.api.ApiMenuService;
import com.supermap.dubbo.api.cloud.api.SysApiMenu;
import com.supermap.dubbo.api.cloud.api.SysApiMenuParams;
import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年05月15 11:42
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/cloud/api")
public class ApiMenuController {

    @Reference(timeout = 100000)
    private ApiMenuService apiMenuService;


    //敏感词过滤
    @GetMapping("/getSensiList")
    @ResponseBody
    public JSONUtils getSensiList(String type,String word,Integer limit){
        return  apiMenuService.getSensiList(type,word,limit);
    }

    //组
    @GetMapping("/getSensiGroupList")
    @ResponseBody
    public JSONUtils getSensiGroupList(){
        return  apiMenuService.getSensiGroupList();
    }
    //添加
    @GetMapping("/putSensi")
    @ResponseBody
    public JSONUtils  putSensi(String type,String word){
        return  apiMenuService.putSensi(type,word);
    }
    //删除
    @DeleteMapping("/delSensi")
    @ResponseBody
    public JSONUtils  delSensi(Integer id){
        return  apiMenuService.delSensi(id);
    }

    //获取所有菜单
    @GetMapping("/getApiMenuList")
    @ResponseBody
    public JSONUtils getApiMenuList(String loginName){
        return  apiMenuService.getApiMenuList(loginName);
    }

    //获取所有菜单打钩
    @GetMapping("/getApiMenuList1")
    @ResponseBody
    public JSONUtils getApiMenuList1(String loginName){
        TreeNodeList list=(TreeNodeList)apiMenuService.getApiMenuList(null).getData();
        TreeNodeList yiyou=(TreeNodeList)apiMenuService.getApiMenuList(loginName).getData();
        List<Integer> ids=new ArrayList<>();
        for(TreeNodeList nodeList:yiyou.getChildren()){
            getCheckedID(ids,nodeList);
        }
        for(Integer integer:ids){
            for(TreeNodeList nodeList:list.getChildren()){
                getCheckedTrue(integer,nodeList);
            }
        }

       return  new JSONUtils().setData(list);
    }
    public void getCheckedTrue(Integer id,TreeNodeList treeNodeList){
        if(id==treeNodeList.getId()){
            treeNodeList.setChecked(true);
        }
        if(treeNodeList.getChildren()!=null&&treeNodeList.getChildren().size()>0){
            for(TreeNodeList nodeList:treeNodeList.getChildren()){
                getCheckedTrue(id,nodeList);
            }
        }
    }
    public void getCheckedID(List<Integer> ids,TreeNodeList treeNodeList){
        ids.add(treeNodeList.getId());
        if(treeNodeList.getChildren()!=null&&treeNodeList.getChildren().size()>0){
            for(TreeNodeList nodeList:treeNodeList.getChildren()){
                getCheckedID(ids,nodeList);
            }
        }

    }

    //查询单个菜单及相关参数
    @GetMapping("/getApiMenuById")
    @ResponseBody
    public JSONUtils getApiMenuById(Integer menuId){
        return  apiMenuService.getApiMenuById(menuId);
    }

    //增加接口
    //1.增加本身
    @PutMapping("/putApiMenu")
    @ResponseBody
    public JSONUtils putApiMenu(@RequestBody SysApiMenu sysApiMenu){
        return  apiMenuService.putApiMenu(sysApiMenu);
    }
    //2.增加参数
    @PutMapping("/putApiMenuParms")
    @ResponseBody
    public JSONUtils putApiMenuParms(@RequestBody SysApiMenuParams sysApiMenuParams){
        return  apiMenuService.putApiMenuParms(sysApiMenuParams);
    }
    //3.修改参数
    @PostMapping("/saveApiMenuParms")
    @ResponseBody
    public JSONUtils saveApiMenuParms(@RequestBody SysApiMenuParams sysApiMenuParams){
        return  apiMenuService.saveApiMenuParms(sysApiMenuParams);
    }
    //4.删除参数
    @DeleteMapping("/delApiMenuParms")
    @ResponseBody
    public JSONUtils delApiMenuParms(Integer id){
        return  apiMenuService.delApiMenuParms(id);
    }
    //删除接口 逻辑删除
    @DeleteMapping("/delApiMenu")
    @ResponseBody
    public JSONUtils delApiMenu(Integer menuId){
        return  apiMenuService.delApiMenu(menuId);
    }
    //修改接口
    @PostMapping("/saveApiMenu")
    @ResponseBody
    public JSONUtils saveApiMenu(@RequestBody SysApiMenu sysApiMenu){
        return  apiMenuService.saveApiMenu(sysApiMenu);
    }


}
